package com.njitzx.controller;

import com.njitzx.config.MinioConfig;
import com.njitzx.constant.FileConstant;
import com.njitzx.entity.vo.Result;
import com.njitzx.service.FileService;
import io.minio.MinioClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;

import java.io.File;


@RestController
@RequestMapping(value = "/file/upload")
@Slf4j
public class UploadController {

    @Autowired
    private FileService fileService;

    @Autowired
    private MinioConfig minioConfig;
    @Value("${minio.endpoint}")
    private String endpoint;
    /**
     * 要有一个文件存储的目录？如果判断文件是否已经存在
     */

    /**
     * file 前端传过来的文件
     */
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
        log.info("开始上传文件");
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File tempFile = File.createTempFile("temp", suffix);
        file.transferTo(tempFile);
        String floadName = fileService.upload(tempFile);
        String url = endpoint + "/" + FileConstant.bucketName + "/" + floadName;
        return Result.success(url);
    }
}
